﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace CPPEI.WebApi
{
    /// <summary>
    /// 存放认证信息（使用asp.net form认证）
    /// </summary>
    public class FormsAuthProvider : IAuthProvider
    {
        public bool Save(string userId, TimeSpan expires)
        {
            System.Web.Security.FormsAuthentication.SetAuthCookie(userId, true);
            return true;
        }

        public void Delete()
        {
            System.Web.Security.FormsAuthentication.SignOut();
        }

        public string GetUserId()
        {
            if (HttpContext.Current.User == null)
            {
                return null;
            }
            return HttpContext.Current.User.Identity.Name;
        }

        public bool IsAuth()
        {
            if (HttpContext.Current.User == null)
            {
                return false;
            }
            return HttpContext.Current.User.Identity.IsAuthenticated;
        }
    }
}
